using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class ContactsDAO
	{
		public const string CONTACTS_GET_BY_ID = "ContactsGetByID";
		public const string CONTACTS_GET_ALL = "ContactsGetAll";
		public const string CONTACTS_INSERT = "ContactsInsert";
		public const string CONTACTS_UPDATE_BY_ID = "ContactsUpdateByID";
		public const string CONTACTS_DELETE_BY_ID = "ContactsDeleteByID";

		public const string CONTACTID = "ContactID";
		public const string NAME = "Name";
		public const string ADDRESS = "Address";
		public const string EMAIL = "Email";
		public const string PHONE = "Phone";
		public const string POSITION = "Position";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public ContactsDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public ContactsDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(CONTACTS_GET_BY_ID);
			try
			{
                Database.AddInParameter(command, CONTACTID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetContactsFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<ContactsDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(CONTACTS_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<ContactsDTO>();
				while (dataReader.Read())
				{
					var item = GetContactsFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(ContactsDTO contacts)
		{
			var command = Database.GetStoredProcCommand(CONTACTS_INSERT);
			try
			{
				Database.AddOutParameter(command, CONTACTID, DbType.String, 36);
				Database.AddInParameter(command, NAME, DbType.String, contacts.Name);
				Database.AddInParameter(command, ADDRESS, DbType.String, contacts.Address);
				Database.AddInParameter(command, EMAIL, DbType.String, contacts.Email);
				Database.AddInParameter(command, PHONE, DbType.String, contacts.Phone);
                Database.AddInParameter(command, POSITION, DbType.Int32, contacts.Position);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, CONTACTID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(ContactsDTO contacts)
		{
			var command = Database.GetStoredProcCommand(CONTACTS_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, CONTACTID, DbType.String, contacts.ContactID);
				Database.AddInParameter(command, NAME, DbType.String, contacts.Name);
				Database.AddInParameter(command, ADDRESS, DbType.String, contacts.Address);
				Database.AddInParameter(command, EMAIL, DbType.String, contacts.Email);
				Database.AddInParameter(command, PHONE, DbType.String, contacts.Phone);
				Database.AddInParameter(command, POSITION, DbType.Int32, contacts.Position);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string contactid)
		{
			var command = Database.GetStoredProcCommand(CONTACTS_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, CONTACTID, DbType.String, contactid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static ContactsDTO GetContactsFromDataReader(IDataRecord dataReader)
		{
			var item = new ContactsDTO();
			try
			{

				item.ContactID = (string.IsNullOrEmpty(Convert.ToString(dataReader[CONTACTID]))) ? "" : Convert.ToString(dataReader[CONTACTID]);
				item.Name = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAME]))) ? "" : Convert.ToString(dataReader[NAME]);
				item.Address = (string.IsNullOrEmpty(Convert.ToString(dataReader[ADDRESS]))) ? "" : Convert.ToString(dataReader[ADDRESS]);
				item.Email = (string.IsNullOrEmpty(Convert.ToString(dataReader[EMAIL]))) ? "" : Convert.ToString(dataReader[EMAIL]);
				item.Phone = (string.IsNullOrEmpty(Convert.ToString(dataReader[PHONE]))) ? "" : Convert.ToString(dataReader[PHONE]);
				item.Position = (string.IsNullOrEmpty(Convert.ToString(dataReader[POSITION]))) ? 0 : Convert.ToInt32(dataReader[POSITION].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
	}
}

